package hk.dao.impl;

import hk.dao.DaoAdmin;
import hk.jdbc.DBUtil;
import hk.pojo.AdminInfo;
import hk.util.PageInfo;

import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/4/11 18:14
 */
public class DaoAdminImpl implements DaoAdmin {
    @Override
    public AdminInfo login(AdminInfo adminInfo) {
        return DBUtil.getSingleObj("select * from admininfo where adminName = ? and password = ?",AdminInfo.class,adminInfo.getAdminName(),adminInfo.getPassword());
    }

    @Override
    public int addAdmin(AdminInfo adminInfo) {
        return DBUtil.update("insert into admininfo (note,password,adminName,state) values (?,?,?,?)",adminInfo.getNote(),adminInfo.getPassword(),adminInfo.getAdminName(),adminInfo.getState());
    }

    @Override
    public AdminInfo checkAdminName(String adminName) {
        return DBUtil.getSingleObj("select * from admininfo where adminName = ?",AdminInfo.class,adminName);
    }

    @Override
    public List<AdminInfo> getAll() {
        return DBUtil.getList("select * from admininfo where state != '0'",AdminInfo.class);
    }

    @Override
    public List<AdminInfo> getAdminList(PageInfo pageInfo) {
        return DBUtil.getList("select * from admininfo where state != '0' limit ?,?",AdminInfo.class,pageInfo.getBeginRow(),pageInfo.getPageSize());
    }

    @Override
    public int getAdminCount() {
        long adminCount = DBUtil.getScala("select count(*) from admininfo where state != '0'");
        return Integer.parseInt(adminCount+"");
    }

    @Override
    public int deleteByAdmin(String id) {
        return DBUtil.update("update admininfo set state = 0 where id = ? ",id);
    }

    @Override
    public AdminInfo selectByAdmin(String id) {
        return DBUtil.getSingleObj("select * from admininfo where id = ?",AdminInfo.class,id);
    }

    @Override
    public int updateByAdmin(AdminInfo adminInfo) {
        return DBUtil.update("update admininfo set adminName = ?,password = ?,note = ? where id = ?",adminInfo.getAdminName(),adminInfo.getPassword(),adminInfo.getNote(),adminInfo.getId());
    }

    @Override
    public int updateByState(String id, String state) {
        if(state.equals("2")){
            return DBUtil.update("update admininfo set state = ? where id = ?",1,id);
        }else{
            return DBUtil.update("update admininfo set state = ? where id = ?",2,id);
        }
    }

    @Override
    public int deleteMore(String[] idList) {
        StringBuffer sql = new StringBuffer();
        sql.append("update admininfo set state = 0 where id in ( ");
        for(int i = 0;i<idList.length;i++){
            if(idList.length-1>i){
                sql.append(idList[i]+",");
            }else{
                sql.append(idList[i]+" )");
            }
        }
        return DBUtil.update(sql+"");
    }
}
